from bs4 import BeautifulSoup
import requests

url ='https://news.baidu.com/'
ua={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'
}
req = requests.get(url,headers=ua)

html = req.content.decode('utf-8')

soup = BeautifulSoup(html,'lxml')

#print('输出格式化的beautifulSoup对象:',soup.prettify())
#print('获取title标签:',soup.title)

#print('获取head标签:',soup.head)

#print('获取第一个a标签:',soup.body.a)

#print('所有名称为a的标签:',soup.find_all('a'))

#print('所有名称为a的标签个数:',len(soup.find_all('a')))

#print('soup的name:',soup.name)

#print('a标签的name:',soup.a.name)

#tag = soup.a
#print('tag的name:',tag.name)
#print('tag的内容:',tag)
#tag.name='b'
#print('修改name后tag的内容:',tag)

#print('href属性的值:',tag['href'])

tag = soup.title
print('Tag对象中包含的字符串:',tag.string)

print('tag.string的类型:',type(tag.string))

print('tag替换前:',tag)

tag.string.replace_with('豌豆PRO')
print('tag替换后:',tag)


print('soup的类型:',type(soup))

print('BeautifulSoup对象的特殊属性name:',soup.name)
print('soup.name的类型:',type(soup.name))

print('BeautifulSoup对象的attribute属性:',soup.attrs)